# -*- encoding: utf-8 -*-
from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User

from merengue.base.managers import BaseContentManager
from merengue.base.models import BaseContent, BaseCategory
from plugins.event.models import Event
from plugins.subscription.models import BaseSubscription
from merengue.section.models import Document

from transmeta import TransMeta


POSITIONS = (
    ('handler', 'Handler'),
    ('stack', 'Stack'),
)

SEX_CHOICES = (
    ('m', _('Male')),
    ('f', _('Femail')),
)

TSHIRT_SIZES = (
    #('XS', 'XS'),
    ('S', 'S'),
    ('M', 'M'),
    ('L', 'L'),
    ('XL', 'XL'),
)

SCORE_CHOICES = tuple([(x, x)for x in range(0,11)])

OPTIONS_CHIRIPONE = (
    ('A', _(u'1 night (63 €)')),
    ('B', _(u'2 nights (73 €)')),
#    ('C', _(u'3 nights (74 €)')),
)


class Technique(BaseContent):
    objects = BaseContentManager()

    class Meta:
        verbose_name = _('technique')
        verbose_name_plural = _('techniques')

    @permalink
    def public_link(self):
        return ('technique_view', [self.slug])


class Player(BaseContent):
    avatar = models.ImageField(upload_to='player_avatars')
    avatar_over = models.ImageField(upload_to='player_avatars', blank=True, null=True)
    sport_number = models.IntegerField(blank=True, null=True)
    birth_place = models.CharField(max_length=200, blank=True, null=True)
    team_position = models.CharField(max_length=100, choices=POSITIONS, blank=True, null=True, db_column='position')
    user = models.ForeignKey(User, verbose_name=_('user'), blank=True, null=True)
    event_attended = models.ManyToManyField(Event,
                                        verbose_name=_('event attended'),
                                        blank=True, null=True)

    objects = BaseContentManager()

    class Meta:
        verbose_name = _('player')
        verbose_name_plural = _('players')
        content_view_template = 'sportteam/player_view.html'

    @permalink
    def public_link(self):
        return ('player_view', [self.slug])


class Chronicle(Document):
    author = models.ForeignKey(Player, verbose_name=_('author'))
    event = models.ForeignKey(Event, verbose_name=_('event'))

    objects = BaseContentManager()

    class Meta:
        verbose_name = _('chronicle')
        verbose_name_plural = _('chronicles')
        content_view_template = 'sportteam/chronicle_view.html'

    @permalink
    def public_link(self):
        return ('chronicle_view', [self.slug])


class TournamentType(BaseCategory):

    class Meta:
        verbose_name = _('tournament type')
        verbose_name_plural = _('tournament types')

    @permalink
    def public_link(self):
        return ('tournament_type_view', [self.slug])


class Tournament(BaseContent):

    tournament_type = models.ForeignKey(TournamentType, verbose_name=_('tournament type'))
    event = models.ManyToManyField(Event,
                                    verbose_name=_('event'),
                                    blank=True, null=True)

    objects = BaseContentManager()

    class Meta:
        verbose_name = _('tournament')
        verbose_name_plural = _('tournaments')

    @permalink
    def public_link(self):
        return ('tournament_view', [self.slug])


class SubscriptionTournament(BaseSubscription):
    sex = models.CharField(verbose_name=_('Gender'), max_length=1, choices=SEX_CHOICES)
    tshirt_size = models.CharField(verbose_name=_('Tshirt Size'), max_length=2, choices=TSHIRT_SIZES)
    height = models.CharField(verbose_name=_('Height'), max_length=100, blank=True, null=True)
    want_a_disc = models.BooleanField(verbose_name=_(u'Do you want a Chiripones disc (8 €)?'), default=False)
    is_vegetarian = models.BooleanField(verbose_name=_('Are you vegetarian?'))
    departure = models.DateTimeField(verbose_name=_('Departure'), blank=True, null=True)
    departure_place = models.CharField(verbose_name=_('Departure place'), max_length=200, blank=True, null=True,
                                       help_text=_("i.e. Sevilla airport or Santa Justa train station"))
    day_of_arrival = models.DateTimeField(verbose_name=_('Arrival'), blank=True, null=True)
    day_of_arrival_place = models.CharField(verbose_name=_('Arrival place'), max_length=200, blank=True, null=True,
                                            help_text=_("i.e. Sevilla airport or Santa Justa train station"))
    number_of_tournamets_played = models.CharField(verbose_name=_('Number of tournaments played'), max_length=200, blank=True, null=True)
    year_played = models.CharField(verbose_name=_('Years playing frisbee'), max_length=200, blank=True, null=True)
    team = models.CharField(verbose_name=_('Team'), max_length=200, blank=True, null=True)
    score = models.IntegerField(verbose_name=_('Level yourself'), max_length=1, blank=True, null=True, choices=SCORE_CHOICES)
    position = models.CharField(verbose_name=_('Position'), max_length=7, blank=True, null=True, choices=POSITIONS)
    has_payed = models.BooleanField(verbose_name=_('has he payed?'))
    options = models.CharField(verbose_name=_('Accomodation'), choices=OPTIONS_CHIRIPONE, max_length=1)
    want_spanish_training = models.BooleanField(verbose_name=_(u'Do you want to participate in Spanish team training? (+50€)'),
                                                help_text=_("Don't forget to fill the form"))

    class Meta:
        verbose_name = _('subscription tournament')
        verbose_name_plural = _('subscriptions tournament')

    @classmethod
    def class_form(cls):
        from plugins.sportteam.forms import SubscriptionTournamentForm
        return SubscriptionTournamentForm
